﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;

using System.IO;


using OgilvyOne.BLL;

public partial class sendMail : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string surName = Server.UrlDecode(Request.QueryString["surName"]);
        string personalName = Server.UrlDecode(Request.QueryString["personalName"]);
        string urserEmail = Request.QueryString["userEmail"];
        string mobile = Request.QueryString["moblie"];
        string email1 = Request.QueryString["friendEmail1"];
        string email2 = Request.QueryString["friendEmail2"];
        string email3 = Request.QueryString["friendEmail3"];
        string friendName1 = Server.UrlDecode(Request.QueryString["friendName1"]);
        string friendName2 = Server.UrlDecode(Request.QueryString["friendName2"]);
        string friendName3 = Server.UrlDecode(Request.QueryString["friendName3"]);
        string lang = Request.QueryString["lang"];
        string op = Request.QueryString["op"];
        int res = 0;
        int dbRes;
        if (string.IsNullOrEmpty(op))
        {
            dbRes = Save(surName, personalName, email1, email2, email3, lang, friendName1, friendName2, friendName3, urserEmail, mobile);
            try
            {
                if (!string.IsNullOrEmpty(email1))
                    res = SendMailTo(email1, surName, personalName, friendName1, lang);
                if (!string.IsNullOrEmpty(email2))
                    res = SendMailTo(email2, surName, personalName, friendName2, lang);
                if (!string.IsNullOrEmpty(email3))
                    res = SendMailTo(email3, surName, personalName, friendName3, lang);
            }
            catch(Exception ex)
            {
            }

            Response.Write(dbRes);
        }
        else
        {
            string result=IsExist(urserEmail).ToString();
            Response.Write(result);
        }
    }
    //发送邮件
    public int SendMailTo(string email, string surName, string personalName,string friendName,string lang)
    {
        string sendHtmlName = (lang == "cn") ? System.Configuration.ConfigurationManager.AppSettings["SendHtmlCNName"] : System.Configuration.ConfigurationManager.AppSettings["SendHtmlENName"];
        string emailbody = MailService.generateEmail(surName, personalName, sendHtmlName, friendName);

        bool flag = MailService.sendMail(email, emailbody);
        if (flag)
            return 200;
        else
            return 0;
    }

    //保存信息
    public int Save(string surName, string personalName, string email1, string email2, string email3, string lang, string friendName1, string friendName2,string friendName3,string userEmail,string mobile)
    {
        int flag = new UserBLL().SaveMail(surName, personalName, email1, email2, email3, lang, friendName1, friendName2, friendName3, userEmail,mobile);
        if (flag > 0)
            return 100;
        else
            return 0;
    }

    public int IsExist(string email)
    {
        int flag = new UserBLL().IsExist(email);
        return flag;
    }
}